package com.ruoyi.word.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.domain.word.CommonWord;
import com.ruoyi.common.domain.word.CommonWordRa;
import com.ruoyi.word.dao.mapper.CommonWordRaMapper;
import com.ruoyi.word.service.ICommonWordRaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommonWordRaServiceImpl implements ICommonWordRaService {

    @Autowired
    private CommonWordRaMapper dao;

    @Override
    public List<CommonWordRa> findByWhereStr(String whereStr, String orderField, String order) {
        return dao.findByWhereStr(whereStr, orderField, order);
    }

    @Override
    public int saveByWord(CommonWord word, List<CommonWordRa> list) {
        dao.deleteByWid(word.getId());
        for (int i = 0; i < list.size(); i++) {
            CommonWordRa d = list.get(i);
            if(StringUtils.isEmpty(d.getName())) {
                continue;
            }
            d.setId(null);
            d.setWid(word.getId());
            d.setWord(word.getName());
            d.setAddTime(new Date());
            d.setSort(i + 1);
            // 设置中线位置
            if(StringUtils.startsWith(d.getName(), "-") && StringUtils.endsWith(d.getName(), "-")) {
                d.setMidline(2);
                d.setName(d.getName().substring(1, d.getName().length() - 1));
            }else if(StringUtils.startsWith(d.getName(), "-")) {
                d.setMidline(-1);
                d.setName(d.getName().substring(1));
            } else if(StringUtils.endsWith(d.getName(), "-")) {
                d.setMidline(1);
                d.setName(d.getName().substring(0, d.getName().length() - 1));
            } else {
                d.setMidline(0);
            }
            dao.insertSelective(d);
        }
        return 1;
    }

    @Override
    public List<CommonWordRa> findByWid(Long wid) {
        return dao.findByWid(wid);
    }

    @Override
    public List<Map<String, Object>> findAllRa() {
        return dao.findAllRa();
    }

    @Override
    public int deleteByWid(Long wid) {
        return dao.deleteByWid(wid);
    }
}
